package ServerClient;
/*
 * Contains the commands to be sent btw server and client 
 */

import java.awt.Point;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.ImageIcon;

/**
 *
 * @author bjorn
 */
public class ClientServerCmd implements Serializable {

    // Command string constants
    public final static int MOVE_FORWARD = 1;
    public final static int MOVE_BACKWARD = 2;
    public final static int TURN_LEFT = 3;
    public final static int TURN_RIGHT = 4;
    public final static int GOTO = 5;
    public final static int STOP = 6;
    public final static int NULL = 0;
    private int command = NULL;
    private Point startxyPos;
    private Point stopxyPos;
    private ImageIcon snapshot;

    /* creates a new command using the constants. if a faulty command string
     * is passed the null command will be generated
     */
    public ClientServerCmd(int cmd) {
        switch (cmd) {
            case ClientServerCmd.STOP:
                command = ClientServerCmd.STOP;
                break;
            case ClientServerCmd.MOVE_FORWARD:
                command = ClientServerCmd.MOVE_FORWARD;
                break;
            case ClientServerCmd.MOVE_BACKWARD:
                command = ClientServerCmd.MOVE_BACKWARD;
                break;
            case ClientServerCmd.TURN_LEFT:
                command = ClientServerCmd.TURN_LEFT;
                break;
            case ClientServerCmd.TURN_RIGHT:
                command = ClientServerCmd.TURN_RIGHT;
                break;
            case ClientServerCmd.NULL:
                command = ClientServerCmd.NULL;
                break;
            case ClientServerCmd.GOTO:
                System.out.print("WARN: Tried to create GOTO command with only one argument. Creating NULL command.");
                command = ClientServerCmd.NULL;
                break;
        }
    }
    /* creates the goto command. if a faulty command string
     * is passed the null command will be generated
     */

    public ClientServerCmd(int cmd, Point start, Point stop, Image snapshot) {
        if (cmd == GOTO) {
            command = GOTO;
            this.startxyPos = start;
            this.stopxyPos = stop;
            this.snapshot = new ImageIcon(snapshot);
        }
    }
    
    public Image getSnapshot() {
        return snapshot.getImage();
    }

    /* returns the coordinates of the goto command
     */
    public Point getStartCoord() {
        if (command == GOTO) {
            return startxyPos;
        }
        return null;
    }
    
    public Point getStopCoord() {
        if (command == GOTO) {
            return stopxyPos;
        }
        return null;
    }
    
    /* returns the command string. Should not be used for other than debug.
     */

    public int getCommand() {
        return command;
    }

    // Determine type
    public boolean isForward() {
        if (command == MOVE_FORWARD) {
            return true;
        }

        return false;
    }

    public boolean isBackward() {
        if (command == MOVE_BACKWARD) {
            return true;
        }

        return false;
    }

    public boolean isTurnleft() {

        if (command == TURN_LEFT) {
            return true;
        }

        return false;
    }

    public boolean isTurnright() {
        if (command == TURN_RIGHT) {
            return true;
        }
        return false;
    }

    public boolean isStop() {
        if (command == STOP) {
            return true;
        }
        return false;
    }

    public boolean isNull() {
        if (command == NULL) {
            return true;
        }
        return false;
    }

    public boolean isGoto() {
        if (command == GOTO) {
            return true;
        }
        return false;
    }
}
